package com.tsing.controller.admin;

import com.tsing.common.result.Result;
import com.tsing.pojo.entity.AdUser;
import com.tsing.service.UserService;
import com.tsing.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * @author 吴阳基
 * @datetime 2023/12/1 10:49
 */
public class UserController {
    private final UserService userService = new UserServiceImpl();

    public Result getById(HttpServletRequest request, HttpServletResponse response) throws SQLException {
       Long id = Long.parseLong(request.getParameter("id"));
        AdUser adUser = userService.getById(id);
        return Result.success(adUser);
    }
    /**
     * 将新的广告用户添加到数据库中。
     *
     * @param请求包含用户数据的 HttpServletRequest 对象
     * @param响应 用于发送响应的 HttpServletResponse 对象
     * @return表示操作结果的 Result 对象
     * @throws SQLException 如果在与数据库交互时发生错误
     */
    public Result add(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        AdUser adUser = new AdUser();
        adUser.setName(request.getParameter("name"));
        adUser.setPassword(request.getParameter("password"));
        adUser.setNickname(request.getParameter("nickname"));
        adUser.setImage(request.getParameter("image"));
        adUser.setPhone(request.getParameter("phone"));
        adUser.setEmail(request.getParameter("email"));

        userService.add(adUser);
        return Result.success();
    }
}
